package com.cee.admin.common;

import com.cee.common.core.context.ProgressBarContext;
import com.cee.common.domain.ProgressBar;
import com.cee.common.web.result.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @author wsz
 * @since 2023/11/20
 */
@RestController
@RequestMapping("/api/progressBar")
@Tag(name = "进度条")
public class ProgressBarController extends BaseController {


    @Operation(summary = "获取key")
    @GetMapping("/key")
    public Result<String> createKey() {
        return success(ProgressBarContext.createKey());
    }

    @Operation(summary = "获取进度百分比")
    @GetMapping("/percentage")
    public Result<Byte> percentage(@RequestParam String key) {
        return success(Optional.ofNullable(ProgressBarContext.getPercentage(key)).map(v->(byte)v.doubleValue()).orElse((byte) 0));
    }

    @Operation(summary = "继续执行")
    @GetMapping("/continue")
    public Result<Boolean> continueRun(@RequestParam String key) {
        ProgressBarContext.continueRun(key);
        return success(true);
    }

    @Operation(summary = "获取状态")
    public Result<ProgressBar> status(@RequestParam String key) {
        return success(ProgressBarContext.status(key));
    }

    @Operation(summary = "提示信息")
    @GetMapping("/confirm")
    public Result<Object> confirm(@RequestParam String key) {
        return success(ProgressBarContext.confirmMsg(key));
    }

    @Operation(summary = "确认信息")
    @GetMapping("/confirmMsg")
    public Result<Object> confirmInfo(@RequestParam String key) {
        return success(ProgressBarContext.confirmMsg(key));
    }

    @Operation(summary = "心跳检测", description = "在阻塞后continue前每隔10秒访问一次，当返回值非true时，说明已经已经断开")
    @GetMapping("/heartbeat")
    public Result<Boolean> heartbeat(@RequestParam String key) {
        return success(ProgressBarContext.heartbeat(key));
    }
}